﻿using Avalonia;
using Project2004.Jigsaw.Utils;

namespace Project2004.Jigsaw.Models
{
    public class Block
    {
        public int X { get; set; }
        public int Y { get; set; }

        public BlockId Id { get; set; }

        public JigsawConfig JigsawConfig { get; set; } = new JigsawConfig();

        public Block()
        {

        }

        public Block(BlockId id, int x, int y)
        {
            Id = id;
            X = x;
            Y = y;
        }

        public Block(Block other)
        {
            Id = other.Id;
            X = other.X;
            Y = other.Y;
            JigsawConfig = new JigsawConfig(other.JigsawConfig);
        }

        /// <summary>
        /// 获得图片来源。
        /// </summary>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        /// <param name="sceneWidth"></param>
        /// <param name="sceneHeight"></param>
        /// <returns></returns>
        public Rect GetImageSourceRect(double size)
        {
            var outterSize = FullConfigJigsawShapeData.GetBoundSizeFromBorderLen(size);
            var extend = FullConfigJigsawShapeData.GetExtend(outterSize);
            var left = size * X;
            var top = size * Y;

            var leftOffset = 0d;
            var topOffset = 0d;

            var width = size;
            var height = size;

            if (JigsawConfig.Left == HoleType.Out)
            {
                leftOffset = -extend;
                width += extend;
            }
            if (JigsawConfig.Top == HoleType.Out)
            {
                topOffset = -extend;
                height += extend;
            }
            if (JigsawConfig.Right == HoleType.Out)
            {
                width += extend;
            }
            if (JigsawConfig.Bottom == HoleType.Out)
            {
                height += extend;
            }

            return new Rect(left + leftOffset, top + topOffset, outterSize, outterSize);
        }



    }
}
